package com.zd.bms.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import com.zd.common.common.utils.FileUtils;

@Controller
@RequestMapping("file")
public class FileController extends BaseController {
	@SuppressWarnings("deprecation")
	@RequestMapping("upload")
	public ModelAndView index(HttpServletRequest req, HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("file/upload");
		MultipartHttpServletRequest multiReq = (MultipartHttpServletRequest) req;
		MultiValueMap<String, MultipartFile> multiFileMap = multiReq.getMultiFileMap();
		MultipartFile imgFile = multiFileMap.getFirst("imgFile");
		String path = req.getRealPath("/");
		String fileName = FileUtils.writeFile(path, imgFile);
		
		//换路径
		String url = req.getRequestURL().toString();
		url = url.replaceAll("/file/upload", "");
		
		mv.addObject("fileName", url+fileName);
		return mv;
	}
}
